'use strict'

import { app, protocol ,ipcMain} from 'electron'
import AppWindow from "./utils/appWindow";
import DataStore from "./utils/DataStore";
const isDevelopment = process.env.NODE_ENV !== 'production'
const winUrl = isDevelopment  ? process.env.WEBPACK_DEV_SERVER_URL:'app://./index.html'
let mainWindow,liveWindow,aboutWindow

const myStore = new DataStore({name:'userData'})
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})
app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  //if (BrowserWindow.getAllWindows().length === 0) new AppWindow({},winUrl)
})


// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {

  mainWindow = new AppWindow({title:'壹深圳大数据智慧运营管理中心',width:970,height:750,maximizable:false},winUrl,false)
  mainWindow.setMenu(null);
  ipcMain.on('open-login-window',(event,args)=>{
    event.preventDefault();
    mainWindow.setSize(470, 423)
    mainWindow.center()
  })
  ipcMain.on('login-success',(event,args)=>{
    event.preventDefault();
    myStore.set('user',args)
    mainWindow.setSize(970, 750)
    mainWindow.loadURL(winUrl)
    mainWindow.center()
  })
  ipcMain.on('logout-user',(event,args)=>{
    event.preventDefault();
    aboutWindow && aboutWindow.close();
    liveWindow && liveWindow.close();
    mainWindow.loadURL(winUrl)
  })
  ipcMain.on('open-about-window',(event,args)=>{
    if(aboutWindow){
      aboutWindow.isMinimized() && aboutWindow.restore()
      aboutWindow.focus()
    } else {
      aboutWindow = new AppWindow({title:'关于',width: 666,minimizable:false, height: 550,parent:mainWindow,maximizable:false},"",false)
      aboutWindow.setMenu(null);
      aboutWindow.loadURL(winUrl + "#/about")
      aboutWindow.on("close",()=>{
        aboutWindow = undefined
      })
    }
  })
  ipcMain.on('open-live-window-by-id',(event,liveUrl) => {
      if(liveWindow){
        liveWindow.isMinimized() && liveWindow.restore()
        liveWindow.focus()
      } else {
        liveWindow = new AppWindow({title:'壹深圳直播',width: 414, height: 750,maximizable:false,resizable:false,parent:mainWindow,titleBarStyle:"default"},"",false)
        liveWindow.setMenu(null);
        liveWindow.loadURL(liveUrl,{userAgent:"Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"})
        liveWindow.on("close",()=>{
          liveWindow = undefined
        })
      }
  })
})
app.on('second-instance',( event ) => {
  if (mainWindow) {
    if (mainWindow.isMinimized()) mainWindow.restore()
    mainWindow.focus()
  }
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}
